package logic

import (
	"context"
	"fmt"
	"newuser/models"

	"newuser/newuserrpc/internal/svc"
	"newuser/newuserrpc/newuserrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NewUserRrcRegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNewUserRrcRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NewUserRrcRegisterLogic {
	return &NewUserRrcRegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 注册
func (l *NewUserRrcRegisterLogic) NewUserRrcRegister(in *newuserrpc.NewUserRrcRegisterRequest) (*newuserrpc.NewUserRrcRegisterResponse, error) {
	// todo: add your logic here and delete this line
	var newuser models.User
	models.DB.Debug().Where("username=?", in.Username).Find(&newuser)
	if newuser.ID != 0 {
		return nil, fmt.Errorf("用户已存在,请重新登录")
	}
	user := models.User{
		Username: in.Username,
		Password: in.Password,
	}
	models.DB.Debug().Create(&user)
	return &newuserrpc.NewUserRrcRegisterResponse{
		Id: int64(user.ID),
	}, nil
}
